<?php
/**
 * 
 * @author tangbl
 * 
 */
class ReplyContentModel extends Model {
	
	//根据对应map条件找到对应的回复记录
	public function replyOneSolevar($map){
		$reply = M("Reply");
		return $reply->where($map)->find();
	}
	
	// 更新内容回复关联记录
	public function upReplyContent($data) {
		if($data){
			return $this->save($data);
		}else{
			return false;
		}
	}
	
	//判断改内容回复关联记录是否存在
	public function exsitsSolevar($content_solevar,$reply_solevar){
		$where = "content_solevar='$content_solevar' and reply_solevar='$reply_solevar'";
		$id    = $this  ->where($where)->getField("id");
		return $id;
					
	}
	
	//根据内容table,获取对应的记录
	public function contentBySolevar($table,$content_solevar){
		$content = M($table);
		$where   = "solevar='$content_solevar'";
		$row     = $content->field("id,title")->where($where)->find();
		$title   = '';
		if($row['id']){
			$title = $row['title'];	
		}
		return $title;
	}
	
	
	//根据内容的solevar 判断该内容的其他事件类型是否已经存在
	public function validReplyBySolevar($solevar){
		$where  = 'content_solevar="'.$solevar.'" and reply_type>3';
		$types  = $this->field("reply_type")->where($where)->select();
		$rows   = array();
		foreach($types as $v){
			$rows[] = $v['reply_type'];
		}
		return $rows;
	}
	
	//根据内容solevar查找回复内容关联表对应的记录
	public function contentByContentSolevar($content_solevar){
		$where = "content_solevar='$content_solevar'";
		return $this->where($where)->select();
	}
	
	//根据回复solevar查找回复内容关联表对应的记录
	public function contentByReplySolevar($reply_solevar){
		$where = "reply_solevar='$reply_solevar'";
		$total = C('WECHAT_REPLY_TOTAL');
		
		return $this->where($where)->order("ordernum desc,addtime desc")->limit($total)->select();
	}
	
	//根据内容编号数组,获取对应的记录
	public function contentByIds($table,$ids){
		$content = M($table);
		$where   = "id in(".implode(",",$ids).")";
		return $content->field("title,solevar")->where($where)->select();
	}
	
	
	//根据对应的内容表和内容的唯一编号查找对应的内容记录
	public function contentByTableId($table,$solevar){
		$content = M($table);
		$where   = "solevar = '".$solevar."'";
		return $content->where($where)->find();
	}
	
	//缓存回复的内容 图文列表
	public function cache_reply_news($solevar){
		$cachename = 'Reply_Cache'.$solevar;
		S($cachename,NULL);
		$lists  =  $this->contentByReplySolevar($solevar);
		$rows   = array();
		$i = 0;
		
		$content_type = C('WECHAT_CONTENT_TYPE');
		foreach($lists as $v){
			$content_table   = $v['content_table'];
			$content_solevar = $v['content_solevar'];
			$content_one     = $this->contentByTableId($content_table,$content_solevar);
			
			if($content_one['id']){
				if($i){
					$image = get_content_thumb($content_solevar,'single',true);
				}else{
					$image = get_content_thumb($content_solevar,'single',false);
				}
				$url   = get_content_url($content_table,$content_one);
				$rows[$i]['Title']       = $content_one['title'];
				$rows[$i]['Description'] = @$content_one['description'];
				$rows[$i]['PicUrl']      = $image;
				$rows[$i]['Url']         = $url;
				
				$array = $content_type[$content_table];
				if(!$array[1]){
					$rows = $this->get_cate_content_list($content_one,$rows[$i],$content_type,$array);
					if(count($rows)>1){
						break;//F("rows5",$rows);
					}
				}
				
				$i++;
			}else{//title为空需要删除
				$this->delete($v['id']);	
			}
		}
		if(!empty($rows))S($cachename,$rows);
	}
	
	//缓存回复的内容 文本信息
	public function cache_reply_text($solevar){
		$cachename = 'Reply_Cache'.$solevar;
		S($cachename,NULL);
		$lists  =  $this->contentByReplySolevar($solevar);
		$rows   = array();
		
		foreach($lists as $v){
			$content_table   = $v['content_table'];
			$content_solevar = $v['content_solevar'];
			$content_one     = $this->contentByTableId($content_table,$content_solevar);
			
			if($content_one['id']){
				$rows['Title']       = $content_one['title'];
				$rows['Description'] = $content_one['description'];
				break;
			}else{
				$this->delete($v['id']);		
			}
		}
		if(!empty($rows))S($cachename,$rows);
	}
	
	//找到对应栏目下的内容列表
	public function get_cate_content_list($content,$row,$content_type,$array){
		$module = $content['module'];
		$pids   = fatherFindChilds($content['id'],"cate");
		$cateid = $array[2];
		$rows[] = $row;
		
		if(array_key_exists($module, $content_type)){
			$model  = M("$module");
			$total  = C('WECHAT_REPLY_TOTAL')-1;
			$lists  = $model->where("$cateid in($pids)")->order("addtime desc")->limit($total)->select();
			
			foreach($lists as $i=>$v){
				$image = get_content_thumb($v['solevar'],'single',true);
				$url   = get_content_url($module,$v);
					
				$rows[$i+1]['Title']       = @$v['title'];
				$rows[$i+1]['Description'] = @$v['description'];
				$rows[$i+1]['PicUrl']      = $image;
				$rows[$i+1]['Url']         = $url;
			}
		}
		return $rows;
	}
	
	//得到栏目下的记录数
	public function contentTotal($content,$content_type){
		$info   = '';
		$module = $content['content_table'];
		$solevar= $content['content_solevar'];
		$array  = $content_type[$module];
		
		if(array_key_exists($module, $content_type) && !$array[1]){
			$wxmenu = $this->contentByTableId($module,$solevar);
			$table  = $wxmenu['module'];
			
			if(array_key_exists($table, $content_type)){
				$pids   = fatherFindChilds($wxmenu['id'],"cate");
				$cateid = $array[2];
				$model  = M("$table");
				$count  = $model->where("$cateid in($pids)")->count();
				$info   = "(".$count.L('cfg_list_itf_recordnum').")";
			}
		}
		return $info;
	}
	
}
?>